<template>
	<view class="shopcart">
		<Hearder :name="'选择订单'"></Hearder>
		<view class="cartshop">
			<view class="cartshop_every" v-for="(item,index) in goods_list" :key="item.goods_id">
				<view class="choosean">
					<image src="/static/images/z44.png" v-if="checkedId.includes(index)" class="icon" @click="checkedicons(index)"></image>
					<image src="/static/images/z43.png" v-else  class="icon" @click="checkedicons(index)"></image>
				</view>
				<view class="detail_con clearfix">
					<image :src="item.image"></image>
					<view class="detail_right">
						<view class="box_top">
							<view class="title">{{item.goods_name}}</view>
						</view>
						<view class="box_bottom">
							<view class="price">￥{{item.pay_price}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="nidata" v-if="goods_list.length==0">
			<image src="../../static/images/noimg.png"></image>
		</view>
		<view class="bot_submit" v-if="goods_list.length!=0">
			<view class="bot_left" @click="checkedicon">
				<image v-if="checked" src="/static/images/z44.png" class="icon" ></image>
				<image src="/static/images/z43.png" v-if="!checked"  class="icon iconcolor"></image>
				<text>全选</text>
			</view>
			<view class="bot_right">
				<view class="total">合计:<text>￥{{totalPrice}}</text></view>
				<view class="submit" @click="paymoney()">确定</view>
			</view>
		</view>
		<shopro-login-modal :showLogin="showLogin" @loginhidden="loginhidden"></shopro-login-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				totalPrice:0,
				checkedId:[],
				goods_list:[],
				checked:false,
				goods_id:[],
				showLogin:false
			}
		},
		onShow() {
			this.getinit()
		},
		methods: {
			getinit(){
				this.request('/order/getList',{dataType: "resold",orderType: 2}).then(res => {
					if(res.data.code==-500){
						this.showLogin=true
					}
					if(res.data.code==1){
						this.goods_list = res.data.data.list
					}	
				})
			},
			//全选
			 checkedicon(){
				this.checked=!this.checked
				if (this.checked) {
					this.totalPrice = 0;
					this.checkedId = [];
					for (let index = 0; index < this.goods_list.length; index++) {
						 this.checkedId.push(index);
					}
					this.goods_id = [];
					this.goods_list.map(item => {
						this.totalPrice += Number(item.pay_price)
						this.goods_id.push(item.order_id)
					})
				} else {
					this.checkedId = [];
					this.totalPrice = 0;
				}
			},
			//每个商品选中
			checkedicons(index){
			   var i=this.checkedId.indexOf(index);
			   if (i !== -1) {
				   this.checkedId.splice(i, 1);
			   } else {
				   this.checkedId.push(index);
			   }
			   if (this.checkedId.length == 0) {
				   this.checked = false;
			   }
			   if (this.checkedId.length == this.goods_list.length) {
				   this.checked = true;
			   }else{
					this.checked = false;
			   }
			   this.calcprice();
			   this.goods_id = [];
			   var that=this;
			   this.checkedId.forEach(ele => {
				   this.goods_id.push(this.goods_list[ele].order_id)
			   });
			 },
			 calcprice() {
			   this.totalPrice = 0;
			   if (this.checkedId.length == 0) {
				   return;
			   }
			   for (let index = 0; index < this.checkedId.length; index++) {
				   let item = this.goods_list[this.checkedId[index]];
				   this.totalPrice += Number(item.pay_price)
			   }
			},
			loginhidden(data){
				this.showLogin=data
			},
			paymoney(route){
				if(this.goods_id.length){
					uni.setStorageSync('goods_order_id',this.goods_id)
					uni.navigateBack()
				}else{
					this.$tip("您还没有选择宝贝哦")
				}
			},
			toSwitch(route){
				uni.switchTab({
				    url: route
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.shopcart{
		.cartshop{
			padding: 0 4%;
			.cartshop_every{
				display: flex;
				align-items: center;
				padding: 24rpx 0;
				.choosean{
					margin-right:20rpx;
					image{
						width: 32rpx;
						height: 32rpx;
					}
				}
				.detail_con{
					width: 90%;
					height: 188rpx;
					box-sizing: content-box;
					margin-left: 10rpx;
					>image{
						float: left;
						width: 188rpx;
						height: 100%;
						border-radius: 10rpx;
					}
					.detail_right{
						float: left;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: calc(100% - 208rpx);
						height: 188rpx;
						margin-left: 20rpx;
						.box_top{
							.title{
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
								font-size: 28rpx;
								line-height: 34rpx;
							}
						}
						.box_bottom{
							display: flex;
							justify-content: space-between;
							align-items: flex-end;
							.price{
								font-weight: 800;
								color: $uni-text-color;
							}
						}
					}
				}
			}
		}
		.bot_submit{
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 98rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #fff;
			box-sizing: border-box;
			z-index: 99;
			border-top: 2rpx solid #eee;
			.bot_left{
				margin-left: 3%;
				image{
					width: 32rpx;
					height: 32rpx;
				}
				text{
					font-size: 32rpx;
					margin-left: 18rpx;
				}
			}
			.bot_right{
				display: flex;
				align-items: center;
				.total{
					font-size: 32rpx;
					margin-right: 30rpx;
					text{
						font-size: 32rpx;
						color: $uni-text-color;
						margin-left: 20rpx;
					}
				}
				.submit{
					background: $uni-text-color;
					width: 180rpx;
					height: 80rpx;
					line-height: 80rpx;
					border-radius: 80rpx;
					color: #fff;
					text-align: center;
					margin:0 20rpx;
					box-sizing: border-box;
				}
			}
		}
	}
</style>
